package com.example.xwadmin.api;

import com.example.xwcommon.annotation.Allow;
import com.example.xwcommon.annotation.Login;
import com.example.xwcommon.entity.Admin;
import com.example.xwcommon.entity.Comment;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.entity.Weibo;
import com.example.xwcommon.service.AdminService;
import com.example.xwcommon.vo.Response;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;

@RestController
public class AdminApi {
    @Resource(name = "adminService")
    private AdminService adminService;

    @Login
    @Allow(roles = {"超级管理员"}, pmses = {"新增管理员"})
    @PostMapping("/api/admin/reg")
    public ResponseEntity<Response<Admin>> reg(@RequestBody Admin admin) {
        Response<Admin> regResponse = adminService.reg(admin);
        regResponse.getData().setAdminLoginPwd(null);
        return ResponseEntity.ok(regResponse);
    }

    @GetMapping("/api/admin/login")
    public ResponseEntity<Response<Admin>> login(String loginName, String loginPwd, HttpServletResponse response) {
        Response<Admin> loginResponse = adminService.login(loginName, loginPwd);
        if(loginResponse.getCode() == 200){
            Admin admin = loginResponse.getData();
            JwtBuilder builder = Jwts.builder();
            builder.setId(UUID.randomUUID().toString());
            builder.setIssuedAt(new Date());
            builder.setExpiration(new Date(System.currentTimeMillis() + 1000 * 20 * 60));
            builder.signWith(SignatureAlgorithm.HS256,"111222");
            builder.claim("admin_id",admin.getAdminId());
            builder.claim("admin_name",admin.getAdminName());
            builder.claim("admin_loginname",admin.getAdminLoginName());
            builder.claim("admin_level",admin.getLevel());
            builder.claim("admin_pmses",admin.getPmses());
            builder.setSubject(admin.getAdminId()+"");
            String jwt=builder.compact();
            System.out.println("jwt:"+jwt);
            response.setHeader("jwttoken",jwt);
            return ResponseEntity.ok(loginResponse);
        }
        return ResponseEntity.status(401).body(loginResponse);
    }

    @PostMapping("/api/admin/banUser")
    public ResponseEntity<Response<User>> banUser(Integer userId) {
        Response<User> userResponse = adminService.banUser(userId);
        if(userResponse.getCode() == 200){
            return ResponseEntity.ok(userResponse);
        }
        return ResponseEntity.status(401).body(userResponse);
    }

    @PostMapping("/api/admin/updateWeiboState")
    public ResponseEntity<Response<Weibo>> updateWeiboState(Integer weiboId, Integer state) {
        Response<Weibo> weiboResponse = adminService.updateWeiboState(weiboId, state);
        if(weiboResponse.getCode() == 200){
            return ResponseEntity.ok(weiboResponse);
        }
        return ResponseEntity.status(500).body(weiboResponse);
    }

    @PostMapping("/api/admin/updateCommentState")
    public ResponseEntity<Response<Comment>> updateCommentState(Integer commentId, Integer state) {
        Response<Comment> commentResponse = adminService.updateCommentState(commentId, state);
        if(commentResponse.getCode() == 200){
            return ResponseEntity.ok(commentResponse);
        }
        return ResponseEntity.status(500).body(commentResponse);
    }
}
